package com.yswee.framework.widget.dialog;

import com.yswee.framework.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class MDialog extends Dialog {
	
	protected boolean mFillWidth;

	protected MDialog(Context context, View layout, int gravity, 
				int anim, boolean fillwidth, boolean cancelable) {
		super(context, R.style.MDialogStyle);
		mFillWidth = fillwidth;
		Window window = getWindow();
	    window.setGravity(gravity);
	    window.setWindowAnimations(anim == 0 ? 
	    		android.R.style.Animation_Dialog : anim);
		setCanceledOnTouchOutside(cancelable);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(layout);
	}

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
	}

	@Override
	protected void onStart() {
		super.onStart();
		if(mFillWidth) {
			Display display = ((WindowManager) getContext().getSystemService(
					Context.WINDOW_SERVICE)).getDefaultDisplay();
			@SuppressWarnings("deprecation")
			int width = display.getWidth();
			getWindow().setLayout(width, LayoutParams.WRAP_CONTENT);
		}
	}

	@Override
	protected void onStop() {
		
	}

	public static interface DlgItemClickListener {
		public abstract void onItemClick(View view, int position, long id);
	}

	public static class AlertDlgBuilder {

		private Context mContext;
		private boolean mAutoClose;
		private int mGravity;
		private boolean mFillWidth;
		private boolean mCancelable;
		
		private View mLayoutView;
		private View mContentView;
		private int mAnimation;
		private int mIcon;
		private CharSequence mTitle;
		private CharSequence mMessage;
		private String mPositive;
		private String mNegative;
		private View.OnClickListener mPositiveListener;
		private View.OnClickListener mNegativeListener;

		public AlertDlgBuilder(Context context) {
			mContext = context;
			mAutoClose = true;
			mGravity = Gravity.BOTTOM;
			mFillWidth = true;
			mCancelable = true;
		}

		public AlertDlgBuilder setAutoClose(boolean flag) {
			mAutoClose = flag;
			return this;
		}

		public AlertDlgBuilder setAnimation(int anim) {
			mAnimation = anim;
			return this;
		}

		public AlertDlgBuilder setGravity(int gravity) {
			mGravity = gravity;
			return this;
		}

		public AlertDlgBuilder setFillWidth(boolean flag) {
			mFillWidth = flag;
			return this;
		}

		public AlertDlgBuilder setCancelable(boolean flag) {
			mCancelable = flag;
			return this;
		}

		public AlertDlgBuilder setLayoutView(int resid) {
			mLayoutView = LayoutInflater.from(mContext).inflate(resid, null);
			return this;
		}

		public AlertDlgBuilder setLayoutView(View view) {
			mLayoutView = view;
			return this;
		}

		public AlertDlgBuilder setContentView(int resid) {
			mContentView = LayoutInflater.from(mContext).inflate(resid, null);
			return this;
		}

		public AlertDlgBuilder setContentView(View view) {
			mContentView = view;
			return this;
		}

		public AlertDlgBuilder setIcon(int resid) {
			mIcon = resid;
			return this;
		}

		public AlertDlgBuilder setTitle(CharSequence title) {
			mTitle = title;
			return this;
		}

		public AlertDlgBuilder setTitle(int resid) {
			String title = null;
			if (resid > 0) {
				title = mContext.getResources().getString(resid);
			}
			return setTitle(title);
		}

		public AlertDlgBuilder setMessage(CharSequence message) {
			mMessage = message;
			return this;
		}

		public AlertDlgBuilder setMessage(int resid) {
			String message = null;
			if (resid > 0) {
				message = mContext.getResources().getString(resid);
			}
			return setMessage(message);
		}

		public AlertDlgBuilder setPositiveButton(String positive, View.OnClickListener l) {
			mPositive = positive;
			mPositiveListener = l;
			return this;
		}

		public AlertDlgBuilder setPositiveButton(int resid, View.OnClickListener l) {
			String positive = null;
			if (resid > 0) {
				positive = mContext.getResources().getString(resid);
			}
			return setPositiveButton(positive, l);
		}

		public AlertDlgBuilder setNegativeButton(String negative, View.OnClickListener l) {
			mNegative = negative;
			mNegativeListener = l;
			return this;
		}

		public AlertDlgBuilder setNegativeButton(int resid, View.OnClickListener l) {
			String negative = null;
			if (resid > 0) {
				negative = mContext.getResources().getString(resid);
			}
			return setNegativeButton(negative, l);
		}

		public MDialog create() {
			if(mLayoutView == null) {
				mLayoutView = LayoutInflater.from(mContext).inflate(R.layout.widget_mdialog, null);
			}
			final MDialog dialog = new MDialog(mContext, mLayoutView, mGravity, mAnimation, mFillWidth, mCancelable);
			// icon
			ImageView imgIcon = (ImageView) dialog.findViewById(android.R.id.icon);
			if(imgIcon != null) {
				if(mIcon > 0) {
					imgIcon.setImageResource(mIcon);
					imgIcon.setVisibility(View.VISIBLE);
				} else {
					imgIcon.setVisibility(View.GONE);
				}
			}
			// title
			TextView tvTitle = (TextView) dialog.findViewById(android.R.id.title);
			if(tvTitle != null) {
				if(!TextUtils.isEmpty(mTitle)) {
					tvTitle.setText(mTitle);
					tvTitle.setVisibility(View.VISIBLE);
				} else {
					tvTitle.setVisibility(View.GONE);
				}
			}
			// contentview & message
			if(mContentView != null) {
				ViewGroup vgContent = (ViewGroup) dialog.findViewById(android.R.id.content);
				if(vgContent != null) {
					vgContent.removeAllViews();
					vgContent.addView(mContentView);
				}
			} else {
				TextView tvMessage = (TextView) dialog.findViewById(android.R.id.message);
				if(tvMessage != null) {
					if(!TextUtils.isEmpty(mMessage)) {
						tvMessage.setText(mMessage);
						tvMessage.setVisibility(View.VISIBLE);
					} else {
						tvMessage.setVisibility(View.GONE);
					}
				}
			}
			// positive button
			final TextView tvOk = (TextView) dialog.findViewById(android.R.id.button1);
			if(tvOk != null) {
				if(!TextUtils.isEmpty(mPositive)) {
					tvOk.setText(mPositive);
					tvOk.setVisibility(View.VISIBLE);
					tvOk.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							if(mPositiveListener != null) {
								mPositiveListener.onClick(v);
							}
							tvOk.setTag(true);
							if(mAutoClose) {
								dialog.dismiss();
							}
						}
					});
				} else {
					tvOk.setVisibility(View.GONE);
				}
			}
			// negative button
			final TextView tvCancel = (TextView) dialog.findViewById(android.R.id.button2);
			if(tvCancel != null) {
				if(!TextUtils.isEmpty(mNegative)) {
					tvCancel.setText(mNegative);
					tvCancel.setVisibility(View.VISIBLE);
					tvCancel.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							if(mNegativeListener != null) {
								mNegativeListener.onClick(v);
							}
							tvCancel.setTag(true);
							if(mAutoClose) {
								dialog.dismiss();
							}
						}
					});
				} else {
					tvCancel.setVisibility(View.GONE);
				}
			}
			
			dialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					if(tvOk.getTag() != null) {
						tvOk.setTag(null);
						return;
					}
					if(tvCancel.getTag() != null) {
						tvCancel.setTag(null);
						return;
					}
					if(mNegativeListener != null) {
						mNegativeListener.onClick(tvCancel);
					}
				}
			});
			
			dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
			return dialog;
		}
	}
}